
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>More examples &#8212; Boost.HigherOrderFunctions 0.6 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/boostbook.css" />
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Point-free style programming" href="point_free.html" />
    <link rel="prev" title="Polymorphic constructors" href="example_polymorphic_constructors.html" /> 
  </head><body>
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86"src="../../_static/boost.png"></td>
</tr></table>

<div class="spirit-nav">
<a accesskey="p"  href="example_polymorphic_constructors.html"><img src="../../_static/prev.png" alt="Prev"></a>
<a accesskey="u" href="intro.html"><img src="../../_static/up.png" alt="Up"></a>
<a accesskey="h" href="../index.html"><img src="../../_static/home.png" alt="Home"></a>
<a accesskey="n"  href="point_free.html"><img src="../../_static/next.png" alt="Next"></a>
</div>
  

    <div class="document">
  <div class="chapter">
      <div class="body" role="main">
        
  <!-- Copyright 2018 Paul Fultz II
     Distributed under the Boost Software License, Version 1.0.
     (http://www.boost.org/LICENSE_1_0.txt)
-->
<div class="section" id="more-examples">
<h1>More examples<a class="headerlink" href="#more-examples" title="Permalink to this heading">¶</a></h1>
<p>As Boost.HigherOrderFunctions is a collection of generic utilities
related to functions, there is many useful cases with the library, but a key
point of many of these utilities is that they can solve these problems with
much simpler constructs than whats traditionally been done with
metaprogramming. Lets take look at some of the use cases for using Boost.HigherOrderFunctions.</p>
<div class="section" id="initialization">
<h2>Initialization<a class="headerlink" href="#initialization" title="Permalink to this heading">¶</a></h2>
<p>The <a class="reference internal" href="../../include/boost/hof/function.html"><span class="doc std std-doc"><code class="docutils literal notranslate"><span class="pre">BOOST_HOF_STATIC_FUNCTION</span></code></span></a> will help initialize function objects at
global scope, and will ensure that it is initialized at compile-time and (on
platforms that support it) will have a unique address across translation
units, thereby reducing executable bloat and potential ODR violations.</p>
<p>In addition, <a class="reference internal" href="../../include/boost/hof/lambda.html"><span class="doc std std-doc"><code class="docutils literal notranslate"><span class="pre">BOOST_HOF_STATIC_LAMBDA_FUNCTION</span></code></span></a> allows initializing a lambda
in the same manner. This allows for simple and compact function definitions
when working with generic lambdas and function adaptors.</p>
<p>Of course, the library can still be used without requiring global function
objects for those who prefer to avoid them will still find the library useful.</p>
</div>
<div class="section" id="projections">
<h2>Projections<a class="headerlink" href="#projections" title="Permalink to this heading">¶</a></h2>
<p>Instead of writing the projection multiple times in algorithms:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">begin</span><span class="p">(</span><span class="n">people</span><span class="p">),</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">end</span><span class="p">(</span><span class="n">people</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="p">[](</span><span class="k">const</span><span class="w"> </span><span class="n">Person</span><span class="o">&amp;</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">Person</span><span class="o">&amp;</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">year_of_birth</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">year_of_birth</span><span class="p">;</span><span class="w"></span>
<span class="w">          </span><span class="p">});</span><span class="w"></span>
</pre></div>
</div>
<p>We can use the <span class="xref myst"><code class="docutils literal notranslate"><span class="pre">proj</span></code></span> adaptor to project <code class="docutils literal notranslate"><span class="pre">year_of_birth</span></code> on the comparison
operator:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">begin</span><span class="p">(</span><span class="n">people</span><span class="p">),</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">end</span><span class="p">(</span><span class="n">people</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="n">proj</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Person</span><span class="o">::</span><span class="n">year_of_birth</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">_</span><span class="p">));</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="ordering-evaluation-of-arguments">
<h2>Ordering evaluation of arguments<a class="headerlink" href="#ordering-evaluation-of-arguments" title="Permalink to this heading">¶</a></h2>
<p>When we write <code class="docutils literal notranslate"><span class="pre">f(foo(),</span> <span class="pre">bar())</span></code>, the standard does not guarantee the order in
which the <code class="docutils literal notranslate"><span class="pre">foo()</span></code> and <code class="docutils literal notranslate"><span class="pre">bar()</span></code> arguments are evaluated. So with <code class="docutils literal notranslate"><span class="pre">apply_eval</span></code> we
can order them from left-to-right:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">apply_eval</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">]{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">foo</span><span class="p">();</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">]{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">bar</span><span class="p">();</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="extension-methods">
<h2>Extension methods<a class="headerlink" href="#extension-methods" title="Permalink to this heading">¶</a></h2>
<p>Chaining many functions together, like what is done for range based libraries,
can make things hard to read:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">transform</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="n">filter</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="n">numbers</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">[](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">[](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">);</span><span class="w"></span>
</pre></div>
</div>
<p>It would be nice to write this:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">numbers</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">filter</span><span class="p">([](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">transform</span><span class="p">([](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
</pre></div>
</div>
<p>The proposal <a class="reference external" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4165.pdf">N4165</a>
for Unified Call Syntax(UFCS) would have allowed a function call of <code class="docutils literal notranslate"><span class="pre">x.f(y)</span></code> to become
<code class="docutils literal notranslate"><span class="pre">f(x,</span> <span class="pre">y)</span></code>. However, this was rejected by the comittee. So instead pipable functions can be
used to achieve extension methods. So it can be written like this:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">numbers</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w"> </span><span class="n">filter</span><span class="p">([](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w"> </span><span class="n">transform</span><span class="p">([](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
</pre></div>
</div>
<p>Now, if some users feel a little worried about overloading the <em>bitwise or</em>
operator, pipable functions can also be used with <a class="reference internal" href="../../include/boost/hof/flow.html"><span class="doc std std-doc"><code class="docutils literal notranslate"><span class="pre">flow</span></code></span></a> like this:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flow</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="n">filter</span><span class="p">([](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">}),</span><span class="w"></span>
<span class="w">    </span><span class="n">transform</span><span class="p">([](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="p">)(</span><span class="n">numbers</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
<p>No fancy or confusing operating overloading and everything is still quite
readable.</p>
</div>
</div>


      </div>
  </div>
      <div class="clearer"></div>
    </div>
    <div class="footer" role="contentinfo">
    <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
    <td align="left"></td>
    <td align="right"><div class="copyright-footer">
            &#169; Copyright 2016, Paul Fultz II.
        
          Created using <a href="http://sphinx-doc.org/">Sphinx</a> 5.2.1.
          <p>Distributed under the Boost Software License, Version 1.0.
          (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 
          <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
          </p>
    </div></td>
    </tr></table>
    </div>
  </body>
</html>